package com.mjh.service.impl;

import com.mjh.bean.Permission;
import com.mjh.bean.User;
import com.mjh.config.handler.AppAccessDeniedHandler;
import com.mjh.exception.ActException;
import com.mjh.mapper.PermissionMapper;
import com.mjh.mapper.RoleMapper;
import com.mjh.mapper.UserMapper;
import com.mjh.model.SecurityUser;
import jakarta.servlet.ServletException;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.util.Set;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询用户信息
        User user  = userMapper.selectByActName(username);

        // 判断用户是否为空
        if (ObjectUtils.isEmpty(user)) {
            throw new UsernameNotFoundException("账号或密码错误");
        }
        if (user.getAccountEnabled()==0){
         throw new ActException("账号不可用");
        }
        if (user.getAccountNoLocked()==0){
            throw new ActException("账号已被锁定");
        }
        if (user.getCredentialsNoExpired()==0){
            throw new ActException("密码以过期");
        }
        if (user.getAccountNoExpired()==0){
            throw new ActException("账号已被禁用");
        }

        //查询当前用户的角色
          Set<String> roleList =   roleMapper.selectRoleByUserId(user.getId());

        // 查询当前用户的权限
        Set<String> permissionList = permissionMapper.selectPermissionByUserId(user.getId());

        // 查询当前用户的菜单集合
        Set<Permission> menuList = permissionMapper.selectMenuListByUserId(user.getId());

        SecurityUser securityUser = SecurityUser.builder()
                .id(user.getId())
                .loginAct(user.getLoginAct())
                .loginPwd(user.getLoginPwd())
                .name(user.getName())
                .phone(user.getPhone())
                .email(user.getEmail())
                .accountNoExpired(user.getAccountNoExpired())
                .credentialsNoExpired(user.getCredentialsNoExpired())
                .accountNoLocked(user.getAccountNoLocked())
                .accountEnabled(user.getAccountEnabled())
                .createTime(user.getCreateTime())
                .createBy(user.getCreateBy())
                .lastLoginTime(user.getLastLoginTime())
                .permissionList(permissionList)
                .roleList(roleList)
                .menuList(menuList)
                .build();
        return securityUser;
    }
}
